package com.danco.training.zaneuskaya.controller;



import java.io.FileWriter;
import java.io.IOException;
import java.io.Serializable;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;

import com.danco.training.zaneuskaya.domain.entity.Book;
import com.danco.training.zaneuskaya.domain.entity.Order;
import com.danco.training.zaneuskaya.domain.entity.storage.Bookshop;
import com.danco.training.zaneuskaya.domain.service.storage.BookStorageService;
import com.danco.training.zaneuskaya.domain.service.storage.OrderStorageService;
import com.danco.training.zaneuskaya.view.IOUtil;





public class OrderController implements Serializable{
	

	/**
	 * 
	 */
	private static final long serialVersionUID = -9033349643981321595L;
	private OrderStorageService orderStorageService = Bookshop.getBookshopInstance().getOrderStorageService();
	private BookStorageService bookStorageService = Bookshop.getBookshopInstance().getBookStorageService();
	
	private BookController bookController = new BookController();

	public void printOrderList() {
		IOUtil.write("Orders in OrderStorage");
		for(Order order:orderStorageService.getOrderList()){
		IOUtil.write(orderInfo(order));
		}
		
	}

	public void printOrderListSortedByOrderDate() {
		IOUtil.write("Orders in OrderStorage sorted by execution date");
		for(Order order:orderStorageService.sortOrdersByOrderDate(orderStorageService.getOrderList())){
			IOUtil.write(orderInfo(order));
			}
		
	}

	public void printExecutedOrders() {
		IOUtil.write("Executed Orders in OrderStorage ");
		for(Order order:orderStorageService.getExecutedOrders(orderStorageService.getOrderList())){
			IOUtil.write(orderInfo(order));
			}
		

	}

	public void printNotExecutedOrders() {
		IOUtil.write("Not Executed Orders in OrderStorage ");
		for(Order order:orderStorageService.getNotExecutedOrders(orderStorageService.getOrderList())){
			IOUtil.write(orderInfo(order));
			}
		
	}

	public void printExecutedOrderListSortedByOrderDate() {
		IOUtil.write("Enter date  from ");
		
		Date d1 = IOUtil.readDate();
		IOUtil.write("Enter date day to");
		
		Date d2 = IOUtil.readDate();
		IOUtil.write("Executed Orders in OrderStorage sorted by execution date ");
		for(Order order:orderStorageService.sortExecutedOrdersByOrderDate(orderStorageService.getOrderList(),d1,d2)){
			IOUtil.write(orderInfo(order));
			}
		

	}

	public void printExecutedOrderListSortedByPrice()  {
IOUtil.write("Enter date  from ");
		
		Date d1 = IOUtil.readDate();
		IOUtil.write("Enter date day to");
		
		Date d2 = IOUtil.readDate();
		IOUtil.write("Executed Orders in OrderStorage sorted by price ");
		for(Order order:orderStorageService.sortExecutedOrdersByPrice(orderStorageService.getOrderList(),d1,d2)){
			IOUtil.write(orderInfo(order));
			}
	}

	public void createOrder(){
		IOUtil.write("Order creation");
		
		Order order = new Order(orderStorageService.generateUnexistedId());
		orderStorageService.addOrder(order);
		IOUtil.write("Enter book Id to add:");
		bookController.printBookList();
		int bookId = IOUtil.readInt();
		
		orderStorageService.addBookToOrder(order,bookStorageService.getBookById(bookId));
		IOUtil.write("Order is created");
		IOUtil.write(orderInfo(order));
		

	}
	
	

	public void printOrderInfo() {
		IOUtil.write("Enter order Id:");
		int id = IOUtil.readInt();
		while (orderStorageService.getOrderById(id)== null){
			IOUtil.write("Order not existed");
			IOUtil.write("Enter order's to show Id");
			id = IOUtil.readInt();
		}
		IOUtil.write(orderInfo(orderStorageService.getOrderById(id)));
	}
	
	
	public String orderInfo(Order i){
		StringBuffer str = new StringBuffer(i.getId() + "  ");
		for (Book book : i.getBooks()) {
			str.append(book.getId());
			str.append("  ");

		}
		str.append("   ");
		str.append(i.isExecuted()+ "  ");
		str.append(i.getTotalPrice());
		return str.toString();
	}

	public void updateOrder(){
		IOUtil.write("Enter order Id for modifying");
		int id = IOUtil.readInt(); 
		while (orderStorageService.getOrderById(id)== null){
			IOUtil.write("Order not existed");
			IOUtil.write("Enter order's Id");
			id = IOUtil.readInt();
		}
		Order order = orderStorageService.getOrderById(id);
		
		IOUtil.write("Change order state : y/n");
		if (!IOUtil.read().equals("y") ){
			order.setExecuted(!order.isExecuted());
		}
		IOUtil.write(orderInfo(order));
		

	}

	public void deleteOrder() {
		IOUtil.write("Enter order Id");
		int id = IOUtil.readInt();
		while (orderStorageService.getOrderById(id)== null){
			IOUtil.write("Order not existed");
			IOUtil.write("Enter order's to show Id");
			id = IOUtil.readInt();
		}
		orderStorageService.rejectOrder(id);

	}

	public void addBookToOrder() {
		IOUtil.write("Enter order Id for modifying");
		int id = IOUtil.readInt(); 
		while (orderStorageService.getOrderById(id)== null){
			IOUtil.write("Order not existed");
			IOUtil.write("Enter order's to show Id");
			id = IOUtil.readInt();
		}
		Order order = orderStorageService.getOrderById(id);
		
		IOUtil.write("Enter book id to add");
		bookController.printBookList();
		int bookId = IOUtil.readInt(); 
		while (bookStorageService.getBookById(bookId)== null){
		
			IOUtil.write("Enter book's to show Id");
			
			bookId = IOUtil.readInt();
		}
		orderStorageService.addBookToOrder(order, bookStorageService.getBookById(bookId));

	}

	public void deleteBookFromOrder() {
		IOUtil.write("Enter order Id for modifying");
		int id = IOUtil.readInt(); 
		while (orderStorageService.getOrderById(id)== null){
			IOUtil.write("Order not existed");
			IOUtil.write("Enter order's to show Id");
			id = IOUtil.readInt();
		}
		Order order = orderStorageService.getOrderById(id);
		
		IOUtil.write("Enter book id to delete");
		int bookId = IOUtil.readInt(); 
		while (bookStorageService.getBookById(bookId)== null){
			IOUtil.write("Book not existed");
			IOUtil.write("Enter book's to show Id");
			
			bookId = IOUtil.readInt();
		}
		orderStorageService.removeBookFromOrder(order, bookStorageService.getBookById(bookId));
		IOUtil.write("Book is deleted from order");
		IOUtil.write(orderInfo(order));

	}

	public void executeOrder() {
		IOUtil.write("Enter order Id for modifying");
		int id = IOUtil.readInt(); 
		while (orderStorageService.getOrderById(id)== null){
			IOUtil.write("Order not existed");
			IOUtil.write("Enter order's to show Id");
			id = IOUtil.readInt();
		}
		orderStorageService.executeOrderById(id);
	}

	public void copyOrder(){
		IOUtil.write("Enter order Id to copy");
		int id = IOUtil.readInt(); 
		
		while (orderStorageService.getOrderById(id) == null){
			IOUtil.write("Order to copy not existed");
			IOUtil.write("Enter order's to copy Id");
			id = IOUtil.readInt();
		}

		try {
			Order copy = orderStorageService.createOrderCopy(orderStorageService.getOrderById(id));
		
	
		IOUtil.write("Order is created");
		IOUtil.write(orderInfo(copy));
		} catch (CloneNotSupportedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void saveOrderListToFile()  {
		 IOUtil.write("Enter directory and file name for saving");
			String file = IOUtil.read();
			if (file.isEmpty()){
				file = "d:\\file1.csv";
				IOUtil.write("Saved by default in d:\\file1.csv ");
				
			}
			orderStorageService.saveOrderListToFile(file);
				
	}
	
	public void readOrderListFromFile(){
		 IOUtil.write("Enter directory and file name ");
			String file = IOUtil.read();
			if (file.isEmpty()){
				file = "d:\\file1.csv";
					
			}
			for (Order or: orderStorageService.readOrders(file)){
			orderStorageService.addOrder(or);//must be updateOrder
			}
	}
	

	
}
